package cn.gailvlun.gll.net;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.gailvlun.gll.net.apartment.member.MemberService;
import cn.gailvlun.gll.net.apartment.room.RoomService;
import cn.gailvlun.gll.net.apartment.task.TaskService;
import cn.gailvlun.gll.net.apartment.template.TemplateService;
import cn.gailvlun.gll.net.base.BaseService;
import cn.gailvlun.gll.net.base.friend.FriendService;
import cn.gailvlun.gll.net.base.invite.InviteService;
import cn.gailvlun.gll.net.base.profile.ProfileService;
import cn.gailvlun.gll.net.cross.CrossService;
import cn.gailvlun.gll.net.emotion.EmotionService;
import cn.gailvlun.gll.net.extra.ExtraService;
import cn.gailvlun.gll.net.group.GroupService;
import cn.gailvlun.gll.net.hotel.HotelService;
import cn.gailvlun.gll.net.interceptor.TokenInterceptor2;
import cn.gailvlun.gll.net.login.LoginService;
import cn.gailvlun.gll.net.pay.PayService;
import cn.gailvlun.gll.net.token.CustomFactory;
import cn.gailvlun.gll.net.wish.WishService;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;



public class HttpMethods {
    private static HttpMethods INSTANCE = null;

    public static HttpMethods getInstance() {
        if (INSTANCE == null) {
            synchronized (HttpMethods.class) {
                if (INSTANCE == null) {
                    INSTANCE = new HttpMethods();
                }
            }
        }
        return INSTANCE;
    }

    private Retrofit mRetrofit;
    private BaseService mBaseService;
    private RoomService mRoomService;
    private MemberService mMemberService;
    private TemplateService mTemplateService;
    private ProfileService mProfileService;
    private InviteService mInviteService;
    private FriendService mFriendService;
    private EmotionService mEmotionService;
    private TaskService mTaskService;
    private ExtraService mExtraService;
    private HotelService mHotelService;
    private WishService mWishService;
    private PayService mPayService;
    private LoginService mLoginService;
    private CrossService mCrossService;
    private GroupService mGroupService;

    Map<Integer, String> mAutoInfos = new HashMap<>();

    private HttpMethods() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .addInterceptor(new TokenInterceptor2(mAutoInfos))
                .addInterceptor(loggingInterceptor)
                .build();

        mRetrofit = new Retrofit.Builder()
                .baseUrl(BuildConfig.SERVER_URL)
                .addCallAdapterFactory(CustomFactory.create(mAutoInfos))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();

        mBaseService = mRetrofit.create(BaseService.class);
        mRoomService = mRetrofit.create(RoomService.class);
        mMemberService = mRetrofit.create(MemberService.class);
        mTemplateService = mRetrofit.create(TemplateService.class);
        mProfileService = mRetrofit.create(ProfileService.class);
        mInviteService = mRetrofit.create(InviteService.class);
        mFriendService = mRetrofit.create(FriendService.class);
        mEmotionService = mRetrofit.create(EmotionService.class);
        mTaskService = mRetrofit.create(TaskService.class);
        mExtraService = mRetrofit.create(ExtraService.class);
        mHotelService = mRetrofit.create(HotelService.class);
        mWishService = mRetrofit.create(WishService.class);
        mPayService = mRetrofit.create(PayService.class);
        mLoginService = mRetrofit.create(LoginService.class);
        mCrossService = mRetrofit.create(CrossService.class);
        mGroupService = mRetrofit.create(GroupService.class);
    }

    public static BaseService getBaseService() {
        return getInstance().mBaseService;
    }

    public static RoomService getRoomService() {
        return getInstance().mRoomService;
    }

    public static MemberService getMemberService() {
        return getInstance().mMemberService;
    }

    public static TemplateService getTemplateService() {
        return getInstance().mTemplateService;
    }

    public static ProfileService getProfileService() {
        return getInstance().mProfileService;
    }

    public static InviteService getInviteService() {
        return getInstance().mInviteService;
    }

    public static FriendService getFriendService() {
        return getInstance().mFriendService;
    }

    public static EmotionService getEmotionService() {
        return getInstance().mEmotionService;
    }

    public static ExtraService getExtraService() {
        return getInstance().mExtraService;
    }

    public static TaskService getTaskService() {
        return getInstance().mTaskService;
    }

    public static WishService getWishService() {
        return getInstance().mWishService;
    }

    public static LoginService getLoginService() {
        return getInstance().mLoginService;
    }

    public static HotelService getHotelService() {
        return getInstance().mHotelService;
    }

    public static PayService getPayService() {
        return getInstance().mPayService;
    }

    public static CrossService getCrossService() {
        return getInstance().mCrossService;
    }

    public static GroupService getGroupService() {
        return getInstance().mGroupService;
    }

    public static void logOut() {
        INSTANCE = null;
    }

}
